import type { ActionType, ProColumns } from '@ant-design/pro-components';
import { PageContainer, ProTable } from '@ant-design/pro-components';
import '@umijs/max';
import { Button, message, Popconfirm, Space } from 'antd';
import React, { useRef } from 'react';
import {
  deleteSysLogininfoUsingPost,
  listSysLogininfoByPageUsingPost,
} from '@/services/api/sysLogininfoController';

/**
 * 系统登录日志页面
 *
 * @constructor
 */
const SysLoginInfoPage: React.FC = () => {
  const actionRef = useRef<ActionType>();

  /**
   * 删除节点
   *
   * @param row
   */
  const handleDelete = async (row: API.SysLogininfo) => {
    const hide = message.loading('正在删除');
    if (!row) return true;
    try {
      await deleteSysLogininfoUsingPost({
        id: row.id as any,
      });
      hide();
      message.success('删除成功');
      actionRef?.current?.reload();
      return true;
    } catch (error: any) {
      hide();
      message.error('删除失败，' + error.message);
      return false;
    }
  };

  /**
   * 表格列配置
   */
  const columns: ProColumns<API.SysLogininfo>[] = [
    {
      title: 'ID',
      dataIndex: 'id',
      valueType: 'text',
      hideInForm: true,
      align: 'center',
    },
    {
      title: '账号',
      dataIndex: 'userAccount',
      valueType: 'text',
      align: 'center',
    },
    {
      title: '登录浏览器',
      dataIndex: 'browser',
      valueType: 'text',
      align: 'center',
    },
    {
      title: '登录IP',
      dataIndex: 'ipaddr',
      valueType: 'text',
      align: 'center',
    },
    {
      title: '登录地址',
      dataIndex: 'loginLocation',
      valueType: 'text',
      align: 'center',
    },
    {
      title: '登录系统',
      dataIndex: 'os',
      valueType: 'text',
      align: 'center',
    },
    {
      title: '消息',
      dataIndex: 'msg',
      valueType: 'textarea',
      align: 'center',
    },
    {
      title: '状态',
      dataIndex: 'status',
      valueEnum: {
        0: {
          text: '成功',
        },
        1: {
          text: '失败',
        },
      },
      align: 'center',
    },
    {
      title: '登录时间',
      sorter: true,
      dataIndex: 'loginTime',
      valueType: 'dateTime',
      hideInSearch: true,
      hideInForm: true,
      align: 'center',
    },
    {
      title: '操作',
      dataIndex: 'option',
      valueType: 'option',
      align: 'center',
      render: (_, record) => (
        <Space size="middle">
          <Popconfirm
            title="删除数据"
            description="您确定要删除此数据吗？"
            onConfirm={() => handleDelete(record)}
            okText="确认"
            cancelText="取消"
          >
            <Button danger>删除</Button>
          </Popconfirm>
        </Space>
      ),
    },
  ];
  return (
    <PageContainer>
      <ProTable<API.SysLogininfo>
        headerTitle={'查询表格'}
        actionRef={actionRef}
        rowKey="key"
        search={{
          labelWidth: 120,
        }}
        pagination={{
          pageSize: 10,
        }}
        request={async (params, sort, filter) => {
          const sortField = Object.keys(sort)?.[0];
          const sortOrder = sort?.[sortField] ?? undefined;

          const { data, code } = await listSysLogininfoByPageUsingPost({
            ...params,
            sortField,
            sortOrder,
            ...filter,
          } as API.SysUserLoginRequest);

          return {
            success: code === 0,
            data: data?.records || [],
            total: Number(data?.total) || 0,
          };
        }}
        columns={columns}
      />
    </PageContainer>
  );
};
export default SysLoginInfoPage;
